﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using VAdvantage.Grids;
using VAdvantage.Editor;
using System.Windows;
using System.Data;
using System.Collections;

namespace VAdvantage.Controls
{
    public  class VCheckBox : CheckBox, IControl
    {

        #region "Declaration"
        bool _mandatory;
        bool _readOnly;
        bool _updatable;
        string _description;
        string _title;
        string _columnName;
        GridField _field;
        int _displayType;
        ContextMenuStrip popupMenu;

        System.Windows.Data.Binding binding;
        #endregion

        public event EventHandler VetoableChangeListener;
        /// <summary>
        /// Constructor
        /// </summary>
        public VCheckBox()
        {
            
        }

        public VCheckBox(string columnName, bool mandatory, bool isReadOnly, bool isUpdateable,
                         String title, String description, bool tableEditor)
        {
           // this.Name = columnName;
            _columnName = columnName;
            _mandatory = mandatory;
            this.IsThreeState = false;
            this.Width = 300;

            if (isReadOnly || !isUpdateable)
            {
                this.IsEnabled = false;
            }
            if (!tableEditor)
            {
                this.Content = title;
                //this.Anchor = AnchorStyles.Left;
            }
            else
            {
                //this.Anchor = AnchorStyles.Top;
            }

            if (_description !=null &&_description.Length > 0)
            {
                ToolTipService.SetToolTip(this, _description);
            }

            // set contextmenu of control
            //this.ContextMenuStrip = popupMenu;
            //this.DataBindings.DefaultDataSourceUpdateMode = DataSourceUpdateMode.Never;

            this.Checked += CheckedChanged;

            this.Unchecked += CheckedChanged;
        }

        void CheckedChanged(object sender, RoutedEventArgs e)
        {
            if (VetoableChangeListener != null)
            {
                VetoableChangeListener(this, EventArgs.Empty);
            }
        }

       



        

        #region "IControl"

        public bool GetIsMandatory()
        {
            return _mandatory;
        }
        /// <summary>
        /// Sets Mandatory
        /// </summary>
        /// <param name="value">bool type value</param>
        public void SetIsMandatory(bool value)
        {
            _mandatory = value;
        }
        public bool GetIsReadonly()
        {
            return _readOnly;
        }
        public GridField GetField()
        {
            return _field;
        }
        public string GetSelectedText()
        {
            return "";
        }

        public void SetField(GridField field)
        {
            _field = field;
            if (_field != null && !_field.GetColumnName().Equals("IsActive")
                && Model.MRole.GetDefault().IsShowPreference())
            {
                //Framework.ValuePreference.AddPreferenceMenuItem(this.preferenceItem_Click, popupMenu);
            }
        }
        public int GetDisplayType()
        {
            return _displayType;
        }
        public void SetDisplayType(int displayType)
        {
            _displayType = displayType;
        }
        public string GetName()
        {
            return this._columnName;
        }
        public object GetValue()
        {
            if (this.IsChecked != null)
            {
                return this.IsChecked.Value;
            }
            return false;
        }

        bool _isSetting = false;
        /// <summary>
        /// property bind to data
        /// </summary>
        //public object BindValue
        //{
        //    get
        //    {
        //        return this.Checked;
        //    }
        //    set
        //    {
        //        if (value != DBNull.Value)
        //        {
        //            _isSetting = true;
        //            this.Checked = Convert.ToBoolean(value);
        //            _isSetting = false;
        //        }
        //    }
        //}



        /// <summary>
        /// Set value
        /// </summary>
        /// <param name="value">value</param>
        public void SetValue(object value)
        {
            if (value != null && value.ToString().ToLower() == "true")
                this.IsChecked = true;
            else if (value != null && value.ToString().ToLower() == "false")
                this.IsChecked = false;
            else
                this.IsChecked = false;
        }
        public string GetText()
        {
            if (this.IsChecked != null && (bool)this.IsChecked)
            {
                return "Yes";
            }
            else
            {
                return "No";
            }
        }
        public string GetDescription()
        {
            return _description;
        }

        private void preferenceItem_Click(object sender, EventArgs e)
        {
            if (Model.MRole.GetDefault().IsShowPreference())
            {
                // opens Value preference window
                //Framework.ValuePreference obj = Framework.ValuePreference.Start(_field, GetValue(), GetText());
                //if (obj != null)
                //{
                //    obj.ShowDialog();
                //}
            }
            return;
        }

        public void SetReadOnly(bool readOnly)
        {
            this.IsEnabled = !readOnly;
        }
        public void SetBackGround(bool value)
        {
            //this.BackColor = CommonFunctions.GetBackGroundColor(value);
        }

        protected void Dispose(bool disposing)
        {
            //base.Dispose(disposing);
            _field = null;
        }



        public void SetName(string name)
        {
            _columnName = name;
        }

        public bool IsDisabled()
        {
            return !this.IsEnabled;
        }

        public void SetDisabled(bool disabled)
        {
            this.IsEnabled = !disabled;
        }

        public int GetTabIndex()
        {
            return this.TabIndex;
        }

        public void SetTabindex(int tabindex)
        {
            this.TabIndex = tabindex;
        }

        public bool Multiline
        {
            get;set;
        }

        public bool Visible
        {
            get
            {
                return this.Visibility == System.Windows.Visibility.Visible;
            }
            set
            {
                this.Visibility = value?System.Windows.Visibility.Visible:System.Windows.Visibility.Collapsed;
            }
        }

        public bool Focused
        {
            get { return this.IsFocused; }
        }

#endregion

        public void Dispose()
        {
            VetoableChangeListener = null;
            _field = null;

            if (popupMenu != null)
            {
                popupMenu.Items.Clear();
            }
            popupMenu = null;
            binding = null;

        }

        public String BindValue
        {
            set
            {
                binding = new System.Windows.Data.Binding(value);
                binding.Mode = System.Windows.Data.BindingMode.TwoWay;
                binding.ValidatesOnDataErrors = true;
                binding.ValidatesOnExceptions = true;
                binding.ValidatesOnNotifyDataErrors = true;
                binding.NotifyOnValidationError = true;

                this.SetBinding(CheckBox.IsCheckedProperty, binding);
                this.BindingValidationError += new EventHandler<ValidationErrorEventArgs>(VCheckBox_BindingValidationError);
            }
}

private

void  VCheckBox_BindingValidationError(object sender, ValidationErrorEventArgs e)
{
 	
}
        
#region dependency Properties

        public static readonly DependencyProperty CheckedStateProperty = DependencyProperty.Register(
  "CheckedState",
  typeof(string),
  typeof(VCheckBox),
  new PropertyMetadata(null,
      new PropertyChangedCallback(OnCheckedPropertyChanged)
  )
);
        public String CheckedState
        {
            get { return (String)GetValue(CheckedStateProperty); }
            set { SetValue(CheckedStateProperty, value); }
        }

        private static void OnCheckedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //if (e.OldValue != e.NewValue)
            //{
                ((VCheckBox)d).OnCheckedPropertyChanged((String)e.OldValue, (String)e.OldValue);
           // }
        }

        private void OnCheckedPropertyChanged(string oldValue, String newValue)
        {
            SetValue(newValue);
        }


#endregion


    }
}
